<template>
  <div class="login-container">
    <el-form ref="login_form" :model="login_form" :rules="login_rules" label-width="100px" hide-required-asterisk>
      <h3 class="login_title">米奇GiaoGiao科技公司</h3>
      <el-form-item label="用户名:" prop="username">
        <el-input v-model="login_form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码:" prop="password">
        <el-input v-model="login_form.password"></el-input>
      </el-form-item>
      <el-form-item label="验证码:" prop="code" style="margin-right: 120px">
        <el-input v-model="login_form.code" placeholder="点击切换验证码"> </el-input>
        <img :src="captchaUrl" @click="updateCaptcha" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onLogin">登陆</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { postRequest } from '@/utils/request.js'
export default {
  name: 'Login',
  data() {
    return {
      //登陆表单
      login_form: {
        username: '',
        password: '',
        code: '',
      },
      //登陆验证
      login_rules: {
        username: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '验证码不能为空', trigger: 'blur' }],
      },
      //验证码图片
      captchaUrl: '/captcha',
    }
  },
  methods: {
    onLogin() {
      this.$refs.login_form.validate((valid) => {
        if (valid) {
          this.postRequest('/login', this.login_form)
            .then((resp) => {
              if (resp) {
                const token = resp.obj.tokenHead + resp.obj.token
                sessionStorage.setItem('token', token)
                //页面跳转
                this.$router.push('/home')
                // let path = this.$route.query.redirect
                // this.$router.replace(path == '/' || path == undefined ? '/home' : path)
              }

              // alert(JSON.stringify(res))
            })
            .catch((error) => {
              alert(error)
            })

          // this.$message({
          //   type: 'success',
          //   message: '登陆成功!',
          // })
        } else {
          // this.$message({
          //   type: 'warning',
          //   message: '登陆失败,请检查账号密码是否正确!',
          // })
          // // this.$router.push('/main')
          return false
        }
      })
    },
    onReset() {},
    //更新验证码图片
    updateCaptcha() {
      //后面加time参数的目的是保证每次都能刷新验证码
      this.captchaUrl = '/captcha?time=' + new Date()
    },
  },
}
</script>

<style lang="less" scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  font-size: 16px;
  background: url('../../assets/login-bg.jpg') no-repeat center;
  .el-form {
    margin-right: 100px;
    width: 600px;
    height: 400px;
    border: 1px solid #eaeaea;
    border-radius: 10px;
    box-shadow: 1px 1px 25px #cac6c6;
    padding: 0 10px;
    background-color: #fff;
    opacity: 0.9;
    .login_title {
      text-align: center;
      font-size: 30px;
      padding: 20px 0;
      font-weight: 700;
    }
    .el-form-item {
      /deep/ .el-form-item__label {
        font-size: 17px;
      }
      //用户名
      &:first-of-type {
        margin-top: 40px;
      }
      &:nth-child(4) {
        /deep/ .el-form-item__content {
          display: flex;
        }
      }
      //按钮
      &:last-child {
        display: flex;
        justify-content: center;
        /deep/ .el-form-item__content {
          margin-left: 0 !important;
        }
        .el-button {
          margin: 20px;
          width: 100px;
          border-radius: 10px;
        }
      }
    }
  }
}
</style>
